
#include <BStyle>
#include <BGroupBox>
#include <BHBoxLayout>
#include <BVBoxLayout>
#include <BGridLayout>
#include <BApplication>

#include "Config.h"
#include "PageSettingDialog.h"

PageSettingDialog::PageSettingDialog(BWidget* parent) : BDialog(parent)
{
	this->setTitle("Setting");

	_descLabel = new BLabel();

	BGridLayout* gridLayout = new BGridLayout();
	{
		_stepSpinBox = new BFloatSpinBox();
		_unitLabel = new BLabel();

		_spreadWidthSpinBox = new BSpinBox(0, 1000);
		_spreadHeightSpinBox = new BSpinBox(0, 1000);

		gridLayout->addWidget(0, 0, new BLabel("scroll step:"));
		gridLayout->addWidget(0, 1, _stepSpinBox);
		gridLayout->addWidget(0, 2, _unitLabel);
		gridLayout->addWidget(1, 0, new BLabel("spread width"));
		gridLayout->addWidget(1, 1, _spreadWidthSpinBox);
		gridLayout->addWidget(2, 0, new BLabel("spread height"));
		gridLayout->addWidget(2, 1, _spreadHeightSpinBox);
	}

	BVBoxLayout* vlayout = new BVBoxLayout(this);
	vlayout->addWidget(_descLabel);
	vlayout->addLayout(gridLayout);

	connect(_stepSpinBox, Signal_EditFinished, &PageSettingDialog::slotStepSpinBoxEditFinished);
	connect(_spreadWidthSpinBox, Signal_EditFinished, &PageSettingDialog::slotSpreadWidthEditFinished);
	connect(_spreadHeightSpinBox, Signal_EditFinished, &PageSettingDialog::slotSpreadHeightEditFinished);

}
PageSettingDialog::~PageSettingDialog()
{

}

void PageSettingDialog::slotStepSpinBoxEditFinished()
{
	float step = _stepSpinBox->value();
	if (Config* config = BApplication::property("config"))
	{
		config->setScrollStep(step);
	}
}
void PageSettingDialog::slotSpreadWidthEditFinished(BObject* object, const BValue& value)
{
	int spreadWidth = value;
	if (Config* config = BApplication::property("config"))
	{
		BSize spreadSize = config->spreadSize();
		spreadSize.setWidth(spreadWidth);
		config->setSpreadSize(spreadSize);
	}
}
void PageSettingDialog::slotSpreadHeightEditFinished(BObject* object, const BValue& value)
{
	int spreadHeight = value;
	if (Config* config = BApplication::property("config"))
	{
		BSize spreadSize = config->spreadSize();
		spreadSize.setHeight(spreadHeight);
		config->setSpreadSize(spreadSize);
	}
}

void PageSettingDialog::showEvent(const BEvent& event)
{
	BDialog::showEvent(event);
	if (Config* config = BApplication::property("config"))
	{
		_stepSpinBox->setValue(config->scrollStep());
		_spreadWidthSpinBox->setValue(config->spreadSize().width());
		_spreadHeightSpinBox->setValue(config->spreadSize().height());
	}
}

void PageSettingDialog::updateEvent(const BEvent& event)
{
	if (_stepSpinBox->value() >= 1)
	{
		_unitLabel->setText("px");
	}
	else
	{
		_unitLabel->setText("%");
	}
}

void PageSettingDialog::styleEvent(const BEvent& event)
{
	BDialog::styleEvent(event);
	const BStyle* style = event.value();
	const BString& text = style->text("description");
	_descLabel->setText(text);
}

